﻿<Window x:Class="TestRunner.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:dw="clr-namespace:DrWPF.Windows.Controls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:testRunner="clr-namespace:TestRunner" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Foreground="#FFF8F6F3"
        FontSize="14"
        ResizeMode="CanResizeWithGrip"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type=testRunner:MainViewModel, IsDesignTimeCreatable=False}"
        Title="NUnit Test Runner" Height="700" Width="1350">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/Icons.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <Style x:Key="TreeViewItemStyle" TargetType="TreeViewItem" >
                <Setter Property="IsExpanded" Value="True" />
                <Setter Property="IsSelected" Value="{Binding IsInitiallySelected, Mode=OneTime}" />
                <Setter Property="KeyboardNavigation.AcceptsReturn" Value="True" />
                <Setter Property="dw:VirtualToggleButton.IsVirtualToggleButton" Value="True" />
                <Setter Property="dw:VirtualToggleButton.IsChecked" Value="{Binding IsChecked}" />

            </Style>
            <Style x:Key="enableWrappingTextBoxFromCheckBox" TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=wrapTextCheckBox}" Value="true">
                        <Setter Property = "TextWrapping" Value="Wrap"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            <sys:String x:Key="logString">[Log]</sys:String>
            <sys:String x:Key="resultString">[Result]</sys:String>
            <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>
            <testRunner:BoolToVisibilityConverterEx x:Key="reverseBooleanToVisibilityConverter" TrueValue="Collapsed" FalseValue="Visible"/>
            <testRunner:RowDetailsVisiblityConverter x:Key="rowDetailsVisiblityConverter"/>
            <testRunner:InverseBooleanConverter x:Key="inverseBooleanConverter"/>
            <HierarchicalDataTemplate 
                x:Key="CheckBoxItemTemplate"
                ItemsSource="{Binding Children, Mode=OneTime}">
                <StackPanel 
                    
                    Orientation="Horizontal" Height="20">
                    <!-- These elements are bound to a FooViewModel object. -->
                    <CheckBox
                        
                        Focusable="False" 
                        IsEnabled="{Binding DataContext.AllowSelectionChange, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                        IsChecked="{Binding IsChecked}" 
                        VerticalAlignment="Center"/>
                    <StackPanel Height="20" Width="20" Margin="0,0,0,0" Orientation="Horizontal">
                        <ProgressBar 
                            Margin="0,0,0,0"
                            Height="16" Width="30">
                            <ProgressBar.Style>
                                <Style TargetType="ProgressBar">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding State}" Value="{x:Static testRunner:RunState.Running}">
                                            <Setter Property="IsIndeterminate" Value="True"/>
                                            <Setter Property="Visibility" Value="{x:Static Visibility.Visible}"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding State}" Value="{x:Static testRunner:RunState.Passed}">
                                            <Setter Property="Visibility" Value="{x:Static Visibility.Collapsed}"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding State}" Value="{x:Static testRunner:RunState.NotExecuted}">
                                            <Setter Property="Visibility" Value="{x:Static Visibility.Collapsed}"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding State}" Value="{x:Static testRunner:RunState.Failed}">
                                            <Setter Property="Visibility" Value="{x:Static Visibility.Collapsed}"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding State}" Value="{x:Static testRunner:RunState.Aborted}">
                                            <Setter Property="Visibility" Value="{x:Static Visibility.Collapsed}"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ProgressBar.Style>
                        </ProgressBar>
                        <Image 
                            Margin="0,0,0,0"
                            Visibility="Visible"
                            Height="16" Width="16"
                            ToolTip="{Binding Result}">
                            <Image.Style>
                                <Style TargetType="{x:Type Image}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding State}" Value="{x:Static testRunner:RunState.Passed}">
                                            <Setter Property="Source" Value="{StaticResource TestPassedIcon}"/>
                                            <Setter Property="Visibility" Value="{x:Static Visibility.Visible}"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding State}" Value="{x:Static testRunner:RunState.Failed}">
                                            <Setter Property="Source" Value="{StaticResource TestFailedIcon}"/>
                                            <Setter Property="Visibility" Value="{x:Static Visibility.Visible}"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding State}" Value="{x:Static testRunner:RunState.Aborted}">
                                            <Setter Property="Source" Value="{StaticResource TestAbortedIcon}"/>
                                            <Setter Property="Visibility" Value="{x:Static Visibility.Visible}"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                    </StackPanel>
                    <ContentPresenter>
                        <ContentPresenter.Content>
                            <TextBlock 
                                Foreground="#FFD5CFC4"
                                FontSize="14"
                                Text="{Binding Name, Mode=OneTime}" 
                                Margin="2,0"/>
                        </ContentPresenter.Content>
                    </ContentPresenter>
                    <Image 
                        ToolTip="Test Can Run Independenty Of All Other Tests"
                        Margin="5,0,0,0"
                        Height="16" Width="16"
                        Source="{StaticResource PinnedIcon}"
                        Visibility="{Binding CanRunIndependently, Converter={StaticResource booleanToVisibilityConverter}}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </ResourceDictionary>
    </Window.Resources>
    <Grid x:Name="mainGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="350" MinWidth="250"/>
            <ColumnDefinition Width="4"/>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.Resources>
            <FrameworkElement x:Key="ProxyElement" DataContext="{Binding}"/>
        </Grid.Resources>
        <ContentControl Visibility="Collapsed" Content="{StaticResource ProxyElement}"/>
        <Menu Grid.ColumnSpan="10" FontSize="14">
            <MenuItem Header="_File">
                <MenuItem Header="Open" Command="ApplicationCommands.Open"/>
                <MenuItem Header="Close All"  Command="ApplicationCommands.Close"/>
                <Separator/>
                <MenuItem Header="Exit" Click="OnExit" IsEnabled="{Binding TestRunning, Converter={StaticResource inverseBooleanConverter}}"/>
            </MenuItem>
            <MenuItem Header="_Test">
                <MenuItem Header="_Run" Command="{Binding RunTestsIndividuallyCommand}" InputGestureText="Ctrl+Shift+R"/>
                <MenuItem Header="Run In _Parallel" Command="{Binding RunTestsWithParallelismCommand}"  InputGestureText="Ctrl+Shift+P"/>
                <MenuItem Header="Run _Multiple Times" Command="{Binding RunTestsMulitpleTimesCommand}"  InputGestureText="Ctrl+Shift+M"/>
                <MenuItem Header="_Stop" Command="{Binding StopTestsCommand}"  InputGestureText="Ctrl+Shift+S"/>
                <MenuItem Header="_Force Stop" Command="{Binding ForceStopTestsCommand}"  InputGestureText="Ctrl+Shift+F"/>
                <Separator/>
                <MenuItem Header="_Mark selected Tests as Independent" Command="{Binding MarkSelectedTestAsIndependent}"/>
                <MenuItem Header="_Un-Mark selected Tests as Independent" Command="{Binding UnMarkSelectedTestAsIndependent}"/>
            </MenuItem>
            <MenuItem Header="T_ools">
                <MenuItem Header="_Options" Click="OnToolsOption" IsEnabled="{Binding TestRunning, Converter={StaticResource inverseBooleanConverter}}"/>
            </MenuItem>
            <MenuItem Header="Help">
                <MenuItem Header="About" Click="OnAbout"/>
            </MenuItem>
        </Menu>
        <GridSplitter Grid.Column="1" Grid.RowSpan="10" ResizeDirection="Columns" Background="Transparent" HorizontalAlignment="Stretch"/>

        <StackPanel Grid.Column="2" Orientation="Horizontal">
            <Button Content="Run" Width="100" Margin="5,0,0,0" Height="30" Command="{Binding RunTestsIndividuallyCommand}"/>
            <Button Content="Run In Parallel" Width="140" Margin="5,0,0,0" Height="30" Command="{Binding RunTestsWithParallelismCommand}"/>
            <Button Content="Run Multiple Times" Width="140" Margin="5,0,0,0" Height="30" Command="{Binding RunTestsMulitpleTimesCommand}"/>
            <Button Content="Stop" Width="75" Margin="5,0,0,0" Height="30" Command="{Binding StopTestsCommand}"/>
            <Button Content="Force Stop (Kill)" Width="140" Margin="5,0,0,0" Height="30" Command="{Binding ForceStopTestsCommand}"/>
        </StackPanel>
        <TreeView 
            x:Name="testTree"
            Grid.Row="1" 
            Grid.Column="0" 
            HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch"
            ItemContainerStyle="{StaticResource TreeViewItemStyle}"
            ItemsSource="{Binding Path=TestViewModels, Mode=OneTime}"
            ItemTemplate="{StaticResource CheckBoxItemTemplate}" 
            VirtualizingStackPanel.IsVirtualizing="True"
            VirtualizingStackPanel.VirtualizationMode="Recycling"
            IsTextSearchEnabled="True">
            <TreeView.ContextMenu >
                <ContextMenu  FontSize="14">
                    <MenuItem Header="Expand All" Click="OnExpandAll"/>
                    <MenuItem Header="Collapse All" Click="OnCollapseAll"/>
                    <Separator/>
                    <MenuItem 
                        Visibility="{Binding CanMarkIndependent, Converter={StaticResource booleanToVisibilityConverter}}"
                        Header="Mark Test As Independent" 
                        Command="{Binding MarkSelectedTestAsIndependent}"
                        >
                    </MenuItem>
                    <MenuItem 
                        Visibility="{Binding CanUnMarkIndependent, Converter={StaticResource booleanToVisibilityConverter}}"
                        Header="Un-Mark Test As Independent" 
                        Command="{Binding UnMarkSelectedTestAsIndependent}"
                        >
                    </MenuItem>
                </ContextMenu>
            </TreeView.ContextMenu>
        </TreeView>

        <TabControl Grid.Row="1" Grid.Column="2" >
            <TabItem Header="Overview">
                <DataGrid ItemsSource="{Binding Results}" 
                    SelectionMode="Single"
                    AutoGenerateColumns="False"
                    RowDetailsVisibilityMode="{Binding Path=DataContext.RunningInLoops, Source={StaticResource ProxyElement}, Converter={StaticResource rowDetailsVisiblityConverter}}"
                    RowDetailsTemplate="{DynamicResource ApplicationPickerRowDetailsTemplate}" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" IsReadOnly="True">

                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Test Name" Binding="{Binding FullName}"/>
                        <DataGridTextColumn Header="Result" Binding="{Binding Result}"/>
                        <DataGridHyperlinkColumn 
                            Header="NUnit Log File" 
                            Binding="{Binding LogPath}" 
                            ContentBinding="{Binding Source={StaticResource logString}}"
                            Visibility="{Binding Path=DataContext.RunningInLoops, Source={StaticResource ProxyElement}, Converter={StaticResource reverseBooleanToVisibilityConverter}}">
                            <DataGridHyperlinkColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <EventSetter Event="Hyperlink.Click" Handler="Hyperlink_OnClick"/>
                                    <Setter Property="ToolTip" Value="{Binding LogPath}"/>
                                    <Setter Property="TextAlignment" Value="Center"/>
                                </Style>
                            </DataGridHyperlinkColumn.ElementStyle>
                        </DataGridHyperlinkColumn>
                        <DataGridHyperlinkColumn 
                            Header="NUnit Result File" 
                            Binding="{Binding ResultPath}" 
                            ContentBinding="{Binding Source={StaticResource resultString}}"
                            Visibility="{Binding Path=DataContext.RunningInLoops, Source={StaticResource ProxyElement}, Converter={StaticResource reverseBooleanToVisibilityConverter}}">
                            <DataGridHyperlinkColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <EventSetter Event="Hyperlink.Click" Handler="Hyperlink_OnClick"/>
                                    <Setter Property="ToolTip" Value="{Binding ResultPath}"/>
                                    <Setter Property="TextAlignment" Value="Center"/>
                                </Style>
                            </DataGridHyperlinkColumn.ElementStyle>
                        </DataGridHyperlinkColumn>
                    </DataGrid.Columns>
                    <DataGrid.Resources>
                        <DataTemplate  x:Key="ApplicationPickerRowDetailsTemplate">
                            <Border CornerRadius="25" Margin="15,5,5,5">
                            <DataGrid 
                                AutoGenerateColumns="False"
                                ItemsSource="{Binding Path=RunResults}" 
                                CanUserSortColumns="False" CanUserDeleteRows="False" CanUserAddRows="False" CanUserResizeRows="False" CanUserReorderColumns="False" 
                                SelectionMode="Single" SelectionUnit="Cell">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Run Number" Binding="{Binding RunNumber}"/>
                                    <DataGridTextColumn Header="Result" Binding="{Binding Result}"/>
                                    <DataGridHyperlinkColumn Header="NUnit Log File" Binding="{Binding LogPath}" ContentBinding="{Binding Source={StaticResource logString}}">
                                        <DataGridHyperlinkColumn.ElementStyle>
                                            <Style TargetType="TextBlock">
                                                <EventSetter Event="Hyperlink.Click" Handler="Hyperlink_OnClick"/>
                                                <Setter Property="ToolTip" Value="{Binding LogPath}"/>
                                                <Setter Property="TextAlignment" Value="Center"/>
                                            </Style>
                                        </DataGridHyperlinkColumn.ElementStyle>
                                    </DataGridHyperlinkColumn>
                                    <DataGridHyperlinkColumn Header="NUnit Result File" Binding="{Binding ResultPath}" ContentBinding="{Binding Source={StaticResource resultString}}">
                                        <DataGridHyperlinkColumn.ElementStyle>
                                            <Style TargetType="TextBlock">
                                                <EventSetter Event="Hyperlink.Click" Handler="Hyperlink_OnClick"/>
                                                <Setter Property="ToolTip" Value="{Binding ResultPath}"/>
                                                <Setter Property="TextAlignment" Value="Center"/>
                                            </Style>
                                        </DataGridHyperlinkColumn.ElementStyle>
                                    </DataGridHyperlinkColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                            </Border>
                        </DataTemplate>
                    </DataGrid.Resources>
                </DataGrid>
            </TabItem>
            <TabItem Header="Output">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <CheckBox 
                            x:Name="wrapTextCheckBox" 
                            Margin="5,0,5,0"
                            ToolTip="Check to wrap long lines in the ouptut below."
                            Content="Enable word wrap"/>
                        <CheckBox 
                            x:Name="removeStackTraceCheckBox" 
                            Margin="5,0,5,0"
                            ToolTip="Check to remove the stack traces from output. This must be done before the tests are run."
                            Content="Remove Stack Trace" 
                            Click="OnRemoveStackTrace"/>
                        <Button Margin="5,0,5,0" Content="Clear" Click="OnClearOutputPane"/>
                        <CheckBox 
                            x:Name="autoScroolCheckBox" 
                            Margin="5,0,5,0"
                            Content="Auto scrool Logs"
                            />
                        <CheckBox 
                            x:Name="showConsoleOutputCheckBox" 
                            Margin="5,0,5,0"
                            Content="Show NUnit Console Output"
                            IsChecked="{Binding ShowNunitConsoleOutput}"
                            />
                        <CheckBox 
                            x:Name="collateTestsCheckBox" 
                            Margin="5,0,5,0"
                            Content="Collate tests"
                            IsChecked="{Binding CollateTests}"
                            ToolTip="When running tests multiple time, run a given test N number of times before moving to other test. If this option is not selected, all the tests are run once, then again for N times."
                            />
                    </StackPanel>
                    <TextBox x:Name="outputTextBlock"
                        Style="{StaticResource enableWrappingTextBoxFromCheckBox}"
                        Grid.Row="1"
                        IsReadOnly="True" 
                        AcceptsReturn="True"
                        VerticalContentAlignment="Top"
                        HorizontalContentAlignment="Left"
                        VerticalAlignment="Stretch"
                        HorizontalAlignment="Stretch"
                             HorizontalScrollBarVisibility="Auto"
                             VerticalScrollBarVisibility="Auto"
                             />
                </Grid>
            </TabItem>
        </TabControl>
        
        </Grid>
</Window>

